"""
Case Type   : 基础功能
Case Name   : 创建系统管理员，分别在public和用户模式下创建sequence
Create At   : 2023/05/08
Owner       : cr13
Description :
    1.创建系统管理员
    2.新建用户分别在用户模式和public下创建序列
    3.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
History     :
"""
import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.constant = Constant()
        self.comshpri = CommonSH('PrimaryDbUser')
        self.sequence = "s_ddl_sequence_046"
        self.user = "u_ddl_sequence_046"

    def test_create_sequence(self):
        text = '----step1:创建系统管理员 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop user if exists {self.user} cascade;create user " \
            f"{self.user} with sysadmin password '{macro.COMMON_PASSWD}';"
        result = self.comshpri.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, result, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, result, '执行失败'
                      + text)

        text = '----step2:新建用户分别在用户模式和public下创建序列 ' \
               'expect:成功----'
        self.log.info(text)
        sql_cmd = f"set current_schema = public;drop sequence if exists " \
            f"{self.sequence};create sequence {self.sequence};"
        result = self.comshpri.execut_db_sql(
            sql_cmd, sql_type=f"-U {self.user} -W '{macro.COMMON_PASSWD}'")
        self.log.info(result)
        self.assertIn(self.constant.SET_SUCCESS_MSG, result, '执行失败' + text)
        self.assertIn(self.constant.DROP_SEQUENCE_SUCCESS_MSG, result,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_SEQUENCE_SUCCESS_MSG, result,
                      '执行失败' + text)
        sql_cmd = f"set current_schema = {self.user};drop sequence if exists " \
            f"{self.sequence};create sequence {self.sequence};"
        result = self.comshpri.execut_db_sql(
            sql_cmd, sql_type=f"-U {self.user} -W '{macro.COMMON_PASSWD}'")
        self.log.info(result)
        self.assertIn(self.constant.SET_SUCCESS_MSG, result, '执行失败' + text)
        self.assertIn(self.constant.DROP_SEQUENCE_SUCCESS_MSG, result,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_SEQUENCE_SUCCESS_MSG, result,
                      '执行失败' + text)

    def tearDown(self):
        text = "----step3:环境清理 expect:成功----"
        self.log.info(text)
        result = self.comshpri.execut_db_sql(
            f"drop user if exists {self.user} cascade;")
        self.log.info(result)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, result, '执行失败' +
                      text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
